package com.qf.swagger2204.controller;

import com.qf.swagger2204.dao.CandidateDao;
import com.qf.swagger2204.dao.VoteDao;
import com.qf.swagger2204.entity.Candidate;
import com.qf.swagger2204.entity.Vote;
import com.qf.swagger2204.service.CandidateService;
import com.qf.swagger2204.service.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/cand")
public class CandidateController {
    long start = 0;
    long end = 0;
    int num = 0;
    @Autowired
    CandidateService candidateService;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    VoteDao voteDao;
    @Autowired
    CandidateDao candidateDao;
    @Autowired
    VoteService voteService;

    @GetMapping("/1")
    public HashMap<String,Object> add(Candidate candidate){
        String name = candidate.getCandidateName();
        Boolean flag = candidateService.selectByName(name);
        HashMap<String, Object> map = new HashMap<>();
        if (candidate!=null&&!flag){
            map.put("code",1);
            map.put("msg","ok");
            candidateDao.insert(candidate);
        }else {
            map.put("code",-1);
            map.put("msg","接口操作失败，请联系运维人员");
        }
        return map;
    }
    @GetMapping("/2")
    public HashMap<String,Object> selectPhone(String phone){
        List<Vote> vote = voteService.select(phone);
        for (Vote vote1 : vote) {
            Integer id = vote1.getCandidateId();
            Candidate candidate = candidateDao.selectById(id);
            String name = candidate.getCandidateName();
            vote1.setCantidateName(name);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",1);
        map.put("msg","ok");
        map.put("data",vote);
        return map;
    }
    @PostMapping("/3")
    public synchronized HashMap<String,Object> vote(String phone,Integer candidateId) {
        List<Candidate> candidates = candidateService.selectAll();
        List<Vote> votes = voteService.selectAll();
        for (Candidate candidate : candidates) {
            Integer totalnum = 0;
            for (Vote vote : votes) {
                if (candidate.getCandidateId() == vote.getCandidateId()) {
                    totalnum++;
                    candidate.setTotalnum(totalnum);
                }
            }
        }
        Candidate candidate1 = candidateService.selectById(candidateId);
        HashMap<String, Object> map = new HashMap<>();
        if (num==0){
                    candidate1.setTotalnum(candidate1.getTotalnum()+1);
                        end = System.currentTimeMillis();
                        candidateService.updateNum(candidate1.getCandidateId(),candidate1.getTotalnum());
                        num++;
        }else {
            start = end;
            end= System.currentTimeMillis();
            if ((end-start) > 6000) {
                candidate1.setTotalnum(candidate1.getTotalnum()+1);
                candidateService.updateNum(candidate1.getCandidateId(),candidate1.getTotalnum());
            }else {
                map.put("code",-1);
                map.put("msg","投太快了哟");
                return map;
            }
        }
        map.put("code",1);
        map.put("msg","ok");
        map.put("data",candidate1);
        return map;
    }
    @GetMapping("/4")
    public HashMap<String,Object> total(){
        List<Candidate> candidates = candidateService.selectAll();

        List<Vote> votes = voteService.selectAll();
        for (Candidate candidate : candidates) {
            Integer totalnum = 0;
            for (Vote vote : votes) {

                if (candidate.getCandidateId()==vote.getCandidateId()) {
                    totalnum++;
                    candidate.setTotalnum(totalnum);
                }
            }
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",1);
        map.put("msg","ok");
        map.put("data",candidates);
        return map;
    }
}
